﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DonutLandLibrary.Robots.Environments;

namespace DonutLandLibrary.Data
{
    public class EnvironmentSquaresData
    {
        public static int AddRobotEnvironmentSquare(Square robotEnvironmentSquare, RobotsEntities entities, int environmentID)
        {
            EnvironmentSquare environmentSquare = new EnvironmentSquare();

            //environmentSquare.ID = robotEnvironmentSquare.ID;
            environmentSquare.PowerSources = robotEnvironmentSquare.PowerSources;
            
            environmentSquare.XCoordinate = robotEnvironmentSquare.XCoordinate;
            environmentSquare.YCoordinate = robotEnvironmentSquare.YCoordinate;
            environmentSquare.CorrosivePatchPresent = robotEnvironmentSquare.CorrosivePatchPresent;

            environmentSquare.EnvironmentID = environmentID;

            entities.AddToEnvironmentSquares(environmentSquare);

            entities.SaveChanges();

            return environmentSquare.ID;
        }

        public static void UpdateRobotEnvironmentSquare(Square robotEnvironmentSquare, RobotsEntities entities, int environmentID)
        {
            EnvironmentSquare environmentSquare = entities.EnvironmentSquares.AsParallel().Where(es => es.ID == robotEnvironmentSquare.ID).FirstOrDefault();
            
            //environmentSquare.ID = robotEnvironmentSquare.ID;
            environmentSquare.PowerSources = robotEnvironmentSquare.PowerSources;

            environmentSquare.XCoordinate = robotEnvironmentSquare.XCoordinate;
            environmentSquare.YCoordinate = robotEnvironmentSquare.YCoordinate;
            environmentSquare.CorrosivePatchPresent = robotEnvironmentSquare.CorrosivePatchPresent;

            environmentSquare.EnvironmentID = environmentID;

            //entities.AddToEnvironmentSquares(environmentSquare);

            entities.SaveChanges();
        }

        public static void UpdateRobotEnvironmentSquare(Square robotEnvironmentSquare, int environmentID)
        {
            RobotsEntities entities = new RobotsEntities();

            EnvironmentSquare environmentSquare = entities.EnvironmentSquares.AsParallel().Where(es => es.ID == robotEnvironmentSquare.ID).FirstOrDefault();

            //environmentSquare.ID = robotEnvironmentSquare.ID;
            environmentSquare.PowerSources = robotEnvironmentSquare.PowerSources;

            environmentSquare.XCoordinate = robotEnvironmentSquare.XCoordinate;
            environmentSquare.YCoordinate = robotEnvironmentSquare.YCoordinate;
            environmentSquare.CorrosivePatchPresent = robotEnvironmentSquare.CorrosivePatchPresent;

            environmentSquare.EnvironmentID = environmentID;

            //entities.AddToEnvironmentSquares(environmentSquare);

            entities.SaveChanges();
        }

        //public static List<int> GetRobotEnvironmentSquare()
        //{
        //    RobotsEntities entities = new RobotsEntities();
        //    List<int> environmentIDs = new List<int>();

        //    List<EnvironmentSquare> environments;// = new List<RobotEnvironment>();

        //    environments = entities.EnvironmentSquares.ToList();

        //    foreach (EnvironmentSquare environment in environments)
        //    {
        //        environmentIDs.Add(environment.ID);
        //    }

        //    return environmentIDs;
        //}

        //public static Square GetRobotEnvironmentSquare(int SquareID)
        //{
        //    RobotsEntities entities = new RobotsEntities();

        //    EnvironmentSquare environmentSquare;
        //    Square robotEnvironmentSquare;


        //    environmentSquare = entities.EnvironmentSquares.Where(re => re.ID == SquareID).FirstOrDefault();

        //    robotEnvironmentSquare = new Square(environmentSquare.PowerSources,
        //                                        environmentSquare.CorrosivePatchPresent,
        //                                        environmentSquare.XCoordinate,
        //                                        environmentSquare.YCoordinate);

        //    return robotEnvironmentSquare;
        //}
    }
}
